export function getAddress() {
  // 获取当前地址
  return new Promise((resolve, reject) => {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(
        (position) => {
          const latitude = position.coords.latitude; // 纬度
          const longitude = position.coords.longitude; // 经度
          const url = `https://restapi.amap.com/v3/geocode/regeo?location=${longitude},${latitude}&key=${"af98f975b0ddfc3b18d60749cec3d55a"}&output=json`;
          //使用高德地图API获取地址
          fetch(url)
            .then((response) => response.json())
            .then((data) => {
              if (data.status === "1") {
                resolve(data.regeocode.formatted_address);
              } else {
                reject(new Error(data.info));
              }
            })
            .catch((error) => reject(error));
        },
        (error) => {
          switch (error.code) {
            case error.PERMISSION_DENIED:
              console.log("用户拒绝了地理位置请求。");
              break;
            case error.POSITION_UNAVAILABLE:
              console.log("位置信息不可用。");
              break;
            case error.TIMEOUT:
              console.log("获取位置超时。");
              break;
            case error.UNKNOWN_ERROR:
              console.log("发生未知错误。");
              break;
          }
        }
      );
    } else {
      console.log("浏览器不支持Geolocation。");
    }
  });
}
